package com.guofen.service.hsmpy.impl;

import com.guofen.dao.HsmMiddleWormMapper;
import com.guofen.pojo.HsmMiddleWorm;
import com.guofen.service.hsmpy.SamplingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SamplingServiceImpl implements SamplingService {

    @Autowired
    private HsmMiddleWormMapper hsmMiddleWormMapper;

    @Override
    public boolean add(Integer id, String is_qualified, String inspection_remarks) {
        HsmMiddleWorm hsmMiddleWorm = hsmMiddleWormMapper.selectByPrimaryKey(id);
        hsmMiddleWorm.setIsQualified(is_qualified);
        hsmMiddleWorm.setInspectionRemarks(inspection_remarks);
        int result = hsmMiddleWormMapper.updateByPrimaryKey(hsmMiddleWorm);
        if(result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> sample(Integer id, Integer num) {
        HsmMiddleWorm hsmMiddleWorm = hsmMiddleWormMapper.selectByPrimaryKey(id);
        String batch = hsmMiddleWorm.getBatch();
        String worm_group = hsmMiddleWorm.getWormGroup();
        String small_bug_breed_box_number = hsmMiddleWorm.getSmallBugBreedBoxNumber();

        List<Map<String, HsmMiddleWorm>> tempList = hsmMiddleWormMapper.sample(batch,worm_group,small_bug_breed_box_number);
        if (tempList.size() <= num)  //不足num个，则全部取出
            return tempList;

        Collections.shuffle(tempList);
        List<Map<String, HsmMiddleWorm>> returnList = tempList.subList(0, num);
        System.out.println(returnList.get(0));
        return returnList;
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getlist(){
        return hsmMiddleWormMapper.getSampleList();
    }
}
